//
//  RNBridgeModule.m
//  ChildrenApp
//
//  Created by 毛政敏 on 2024/7/26.
//

#import "RNBridgeModule.h"
#import <React/RCTLog.h>
#import <UIKit/UIKit.h>
@implementation RNBridgeModule
RCT_EXPORT_MODULE();
// 回调函数的方法
RCT_EXPORT_METHOD(createEvent: (NSString *)name callback:(RCTResponseSenderBlock)callback){
  NSArray *events = [[NSArray alloc] initWithObjects:@"张三", @"李四", nil];
  NSInteger eventId = 1;
  RCTLogInfo(@"Pretending to create an event %@", name);
  callback(@[@(eventId), events]);
}
// promise的方法
RCT_EXPORT_METHOD(promiseMethod: (NSString *)param1 resolver: (RCTPromiseResolveBlock)resolve rejecter: (RCTPromiseRejectBlock)reject){
  NSDictionary *obj = @{@"a": @12, @"b": @"string", @"c": @[@1, @2]};
  resolve(obj);
}
// 使用原生的toast
//RCT_EXPORT_METHOD(showToastWithMessage: (NSString *) message duration: (NSInteger)duration position: (NSString *)position){
//  UILabel *toastLabel = [[UILabel alloc] initWithFrame:CGRectZero];
//  toastLabel.text = message;
//  toastLabel.textAlignment = NSTextAlignmentCenter;
//  toastLabel.numberOfLines = 0;
//  toastLabel.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
//  toastLabel.textColor = [UIColor whiteColor];
//  toastLabel.layer.cornerRadius = 5.0;
//  toastLabel.layer.masksToBounds = YES;
//  // 根据位置设置toast的frame
//  CGRect screenRect = [[UIScreen mainScreen] bounds];
//  CGFloat screenWidth = screenRect.size.width;
//  CGFloat screenHeight = screenRect.size.height;
//  CGFloat toastWidth = 200.0;
//  CGFloat toastHeight = [toastLabel sizeThatFits:CGSizeMake(toastWidth, CGFLOAT_MAX)].height;
//  CGFloat x = (screenWidth - toastWidth) / 2.0;
//  CGFloat y = 0.0;
//  if ([position isEqualToString:@"top"]) {
//    y = 50.0; // 距离屏幕顶部50点
//  } else if ([position isEqualToString:@"center"]) {
//    y = (screenHeight - toastHeight) / 2.0; // 垂直居中
//  } else if ([position isEqualToString:@"bottom"]) {
//    y = screenHeight - toastHeight - 50.0; // 距离屏幕底部50点
//  }
//  toastLabel.frame = CGRectMake(x, y, toastWidth, toastHeight);
//  
//  // 将toast添加到keyWindow上
//  UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
//  [keyWindow addSubview:toastLabel];
//  
//  // 使用block动画模拟toast显示和消失的效果
//  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//  [UIView animateWithDuration:0.3 animations:^{
//    toastLabel.alpha = 0.0;
//  } completion:^(BOOL finished) {
//      [toastLabel removeFromSuperview];
//    }];
//  });
//}
@end
